import { compose, on, map } from './utils'
import { pickIfExist } from './ts_utils'
import { doesCommandSupportTargetOptions } from './command'

export const normalizeCommand = (command: any) => {
  const result = pickIfExist(['id', 'cmd', 'target', 'value', 'targetOptions', 'description', 'children'], command)

  if (!doesCommandSupportTargetOptions(result.cmd)) {
    delete result.targetOptions
  }

  return result
}

export const normalizeTestCase = (testCase: any) => {
  const intermediate = compose(
    on('data'),
    on('commands'),
    map
  )(normalizeCommand)(testCase)

  return pickIfExist(['id', 'data', 'name', 'path', 'children'], intermediate)
}

export const commandWithoutBaseUrl = (baseUrl: string) => (command: any) => {
  if (command.cmd !== 'open') return command

  return {
    ...command,
    target: (baseUrl + '/' + command.target).replace(/\/+/g, '/')
  }
}

export const eliminateBaseUrl = (testCase: any) => {
  if (!testCase.baseUrl) return testCase
  return compose(
    on('data'),
    on('commands'),
    map
  )(commandWithoutBaseUrl(testCase.baseUrl))(testCase)
}
